<?php
/*! \brief
 * This is WFWidgetComponentInfoXmlManager class
 * This is the Class that manipulate file /path/to/repository/widgets_new_info.xml
 * \author William Su
 */
class WFWidgetComponentInfoXmlManager
{
    /**
     * @var WF
     */
    protected $objFramework;
    protected $strPathWidgetComponentInfoXmlFromWpCurrent;
    protected $strPathWidgetComponentInfoXmlFromWidgetsNew;
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
        $this->strPathWidgetComponentInfoXmlFromWpCurrent = $this->objFramework->getWPHome() . '/widgetComponent/widget_component_info.xml';
        $this->strPathWidgetComponentInfoXmlFromWidgetsNew = $this->objFramework->getDocumentRoot() . '/repository/widgets_new/widgetComponent/widget_component_info.xml';
    }
    /**
     *\brief Description
     * - This method will get assoc array value from /path/to/repository/widget_new/widget_info.xml
     *\par Variable
     * - EX : $assocProductIdWithWidgetVersionFromWidgetsNew
     * array (
          '15.2' => 
          array (
            '@attributes' => 
            array (
              'id' => '15.2',
              'name' => 'OfficeScan Server',
            ),
            'widget_component_version' => '1.0',
          ),
          '15.1' => 
          array (
            '@attributes' => 
            array (
              'id' => '15.1',
              'name' => 'OfficeScan Client',
            ),
            'widget_component_version' => '2.0',
          ),
        )
     */
    public function getAssocProductIdWithWidgetVersionFromWidgetsNew(){
        $ret = array();
        $arrsProductType = array();
        $isFileExists = $this->objFramework->getIOFactory()->getFile()->isFileExists($this->strPathWidgetComponentInfoXmlFromWidgetsNew);
        if(true === $isFileExists){
            $elArrsProductInfo = new SimpleXMLElement($this->strPathWidgetComponentInfoXmlFromWidgetsNew, null, true);
            $elArrsProductType = $elArrsProductInfo->xpath('/widget_component_info/product_types/product_type');
            $arrsProductType = $this->objFramework->getTypeFactory()->getObject()->convertObject2Array($elArrsProductType);
        }
        foreach($arrsProductType as $arrProductType){
            $ret[$arrProductType['@attributes']['id']] = $arrProductType;
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get widget_version value from /path/to/repository/widgetPool/wpCurrent/widget/widget_info.xml
     * \par Variable
     * - EX : $assocProductIdWithWidgetVersionFromWpCurrent
     * array (
          '15.2' => 
          array (
            '@attributes' => 
            array (
              'id' => '15.2',
              'name' => 'OfficeScan Server',
            ),
            'widget_component_version' => '1.0',
          ),
          '15.1' => 
          array (
            '@attributes' => 
            array (
              'id' => '15.1',
              'name' => 'OfficeScan Client',
            ),
            'widget_component_version' => '2.0',
          ),
        )
     */
    public function getAssocProductIdWithWidgetVersionFromWpCurrent(){
        $ret = array();
        $arrsProductType = array();
        $isFileExists = $this->objFramework->getIOFactory()->getFile()->isFileExists($this->strPathWidgetComponentInfoXmlFromWpCurrent);
        if(true === $isFileExists){
            $elArrsProductInfo = new SimpleXMLElement($this->strPathWidgetComponentInfoXmlFromWpCurrent, null, true);
            $elArrsProductType = $elArrsProductInfo->xpath('/widget_component_info/product_types/product_type');
            //var_dump($elWidgetsNew);exit();
            $arrsProductType = $this->objFramework->getTypeFactory()->getObject()->convertObject2Array($elArrsProductType);
        }
        foreach($arrsProductType as $arrProductType){
            $ret[$arrProductType['@attributes']['id']] = $arrProductType;
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get arr product_type value from /path/to/repository/widget_new/widget_info.xml
     *\par Variable
     * - EX : $arrProductTypeFromWidgetsNewByProductId
     * array (
          '@attributes' => 
          array (
            'id' => '15.1',
            'name' => 'OfficeScan Client',
          ),
          'widget_component_version' => '2.0',
        )
     */
    public function getArrProductTypeFromWidgetsNewByProductId($mixProductId){
        $ret = array();
        $arrsProductType = array();
        $isFileExists = $this->objFramework->getIOFactory()->getFile()->isFileExists($this->strPathWidgetComponentInfoXmlFromWidgetsNew);
        if(true === $isFileExists){
            $elArrsProductInfo = new SimpleXMLElement($this->strPathWidgetComponentInfoXmlFromWidgetsNew, null, true);
            $elArrsProductType = $elArrsProductInfo->xpath('/widget_component_info/product_types/product_type');
            $arrsProductType = $this->objFramework->getTypeFactory()->getObject()->convertObject2Array($elArrsProductType);
        }
        foreach($arrsProductType as $arrProductType){
            if($mixProductId == $arrProductType['@attributes']['id']){
                $ret = $arrProductType;
                break;
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will update widget_component_info->product_types->product_type[@id=$mixProductId]->widget_component_version value to /path/to/repository/widgetPool/wpLogId/widgetComponent/widget_component_info.xml
     */
    public function updateWpLogIdWidgetComponentVersionByProductId($intWpLogId, $mixProductId, $strWidgetComponentVersion){
        $strPathWidgetComponentInfoXmlFromWpLogId = $this->objFramework->getDocumentRoot() . '/repository/widgetPool/wp'.$intWpLogId.'/widgetComponent/widget_component_info.xml';
        $elWidgetComponentInfo = new SimpleXMLElement($strPathWidgetComponentInfoXmlFromWpLogId, null, true);
        $elProductType = $elWidgetComponentInfo->xpath('/widget_component_info/product_types/product_type[@id="' . $mixProductId . '"]');
        $elProductType[0]->widget_component_version = $strWidgetComponentVersion;
        $strContentXml = $elWidgetComponentInfo->asXML();
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($strPathWidgetComponentInfoXmlFromWpLogId, 'w+', $strContentXml);
    }
    /**
     *\brief Description
     * - This method will add below tag to /path/to/repository/widgetPool/wpLogId/widgetComponent/widget_component_info.xml
     * <product_type id="1001.2" name="SPS">
            <widget_component_version>1.0</widget_component_version>
        </product_type>
     */
    public function addWpLogIdProductTypeElementByArrProductType($intWpLogId, $arrProductType){
        $strPathWidgetComponentInfoXmlFromWpLogId = $this->objFramework->getDocumentRoot() . '/repository/widgetPool/wp'.$intWpLogId.'/widgetComponent/widget_component_info.xml';
        $elWidgetComponentInfo = new SimpleXMLElement($strPathWidgetComponentInfoXmlFromWpLogId, null, true);
        $elProductTypes = $elWidgetComponentInfo->xpath('/widget_component_info/product_types');
        $elProductType = $elProductTypes[0]->addChild('product_type');
        $elProductType->addAttribute('id', $arrProductType['@attributes']['id']);
        $elProductType->addAttribute('name', $arrProductType['@attributes']['name']);
        $elProductType->addChild('widget_component_version', $arrProductType['widget_component_version']);

        $strContentXml = $elWidgetComponentInfo->asXML();
        
        // reformat xml output
        $dom = $this->objFramework->getDomFactory()->getDOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($strContentXml);
        $strContentXml = $dom->saveXML();
        
        $this->objFramework->getIOFactory()->getFile()->writeDataSafe($strPathWidgetComponentInfoXmlFromWpLogId, 'w+', $strContentXml);
    }
}